package com.wangshen.administrator.ygqq.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.google.gson.Gson;
import com.wangshen.administrator.ygqq.R;
import com.wangshen.administrator.ygqq.adapter.ExpandLvAdapter;
import com.wangshen.administrator.ygqq.bean.AllGoodsBean;
import com.wangshen.administrator.ygqq.contents.UrlStrings;
import com.wangshen.administrator.ygqq.utils.ToRequestBody;

import java.io.IOException;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2016/7/25.
 */
public class AllFragment extends Fragment {
    private Context mContext;
    private ExpandableListView expandableListView;
    private List<AllGoodsBean.AllClassArrayBean> mList;
    private ExpandLvAdapter adapter;
    private PtrClassicFrameLayout ptr;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(getActivity()).inflate(R.layout.fragment_all,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        expandableListView = (ExpandableListView) view.findViewById(R.id.expandableListView);
        adapter = new ExpandLvAdapter(mContext , mList);
        loadData();
        expandableListView.setAdapter(adapter);

        ptr = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_all_left);
        ptr.setResistance(1.5f);
        //是否是下拉刷新
        //true:下拉到一定位置刷新
        //false:释放刷新
        ptr.setPullToRefresh(false);
        //头部高度的几倍再去刷新
        ptr.setRatioOfHeaderHeightToRefresh(1.5f);
        //刷新的时候，是否保持头部
        ptr.setKeepHeaderWhenRefresh(true);
        //恢复到头部的位置的时长
        ptr.setDurationToClose(200);
        //头部整体关闭的时常
        ptr.setDurationToCloseHeader(1000);

        ptr.setPtrHandler(new PtrDefaultHandler() {
          /*  @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return true;
            }*/

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                ptr.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadData();
                        ptr.refreshComplete();
                    }
                },3000);
            }
        });
    }

    private void loadData() {
        OkHttpClient okhttp = new OkHttpClient();
        FormBody.Builder builder = new FormBody.Builder();
        RequestBody body = new ToRequestBody().toRequestBody(builder , UrlStrings.CLASS_LIST_BODY);
        final Request request = new Request.Builder()
                .url(UrlStrings.CLASS_LIST)
                .post(body).build();
        Call call = okhttp.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body1 = response.body();
                final String json = body1.string();
                final AllGoodsBean allGoodsBean = new Gson().fromJson(json, AllGoodsBean.class);
                Log.i("TAG", "onResponse:----------- "+allGoodsBean.getAll_class_array().get(0).getName());
                Log.i("TAG", "onResponse:----------- "+allGoodsBean.getAll_class_array().get(3).getName());
                Log.i("TAG", "onResponse:----------- "+allGoodsBean.getAll_class_array().get(8).getName());
                Log.i("TAG", "onResponse:----------- "+allGoodsBean.getAll_class_array().get(15).getName());

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.setData(allGoodsBean.getAll_class_array());
                    }
                });
            }
        });
    }
    Handler handler = new Handler();
}
